﻿/* 
 * Since 2008 - 2012
 */

package com.hh.bss.customer.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import com.hh.bss.base.*;
import com.hh.bss.common.ConstantDic;
import com.hh.bss.common.MapObjectRowMapper;
import com.hh.bss.common.page.*;
import com.hh.bss.common.page.impl.*;
import com.hh.bss.customer.dao.*;
import com.hh.bss.customer.entity.*;
import com.hh.bss.customer.service.*;
import com.hh.bss.customer.vo.query.*;
import com.hh.bss.talent.entity.Cv;
import com.hh.bss.talent.service.CvService;
import com.hh.bss.util.*;




@Service
@Transactional
public class CustomerContacterServiceImpl extends BaseBusinessService<CustomerContacter,java.lang.Integer>  implements CustomerContacterService<CustomerContacter,java.lang.Integer>{

	/**检查 是否存在同类型的客户联系人*/
	public int checkCustomerRoleCount(String custId, String role) {
		
		return customerContacterDao.checkCustomerRoleCount(custId, role);
	}
	private CustomerContacterDao customerContacterDao;
	private CvService cvService;
	private CompanyService companyService;
	/**增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,请注意大小写*/
	public void setCustomerContacterDaoImpl(CustomerContacterDao dao) {
		this.customerContacterDao = dao;
	}
	public void setCvServiceImpl(CvService service) {
		this.cvService = service;
	}
	public Map getMapById(Integer id) {
		//this.cvService = service;
		StringBuffer sql = new StringBuffer();
		List values = new ArrayList();
		sql.append(" select * from ( ");
		sql.append(" select 	a.id,a.cust_id custId,a.role,a.cv_id cvId,b.cv_name cvName,a.comment,a.creator,a.create_dt createDt,a.modifier,a.modified_dt modifiedDt from customer_contacter a left join cv b on a.cv_id=b.id where a.id=? ");			
		sql.append(" ) t ");
		values.add(id);
		
		
		List l= customerContacterDao.getJdbcDao().findByQuery(sql.toString(),values.toArray(), new MapObjectRowMapper());
		Map m=new HashMap();
		if (l!=null&&l.size()>0) m=(Map)l.get(0);
		return m;
	}
	
	public void setCompanyServiceImpl(CompanyService service) {
		this.companyService = service;
	}
	public EntityDao getEntityDao() {
		return this.customerContacterDao;
	}
	public void save(CustomerContacter customerContacter){
		customerContacterDao.save(customerContacter);
		//更新简历人选类型
		
		//人选加入公司并更新简历
		
//    	2020 BD联系人
//    	2021 合作决策人
//    	2022 职位联系人
//    	2023 收款联系人
    	String roleId="";
    	String cvType="";
    	//CT-BD联系人 CP- 合作决策人JC- 职位联系人 FC-收款联系人
//    	if ("2020".equals(entity.getCvType())) roleId=ConstantDic.CUSTOMER_CONTACTER_TYPE_CONTACTER;
//    	if ("2021".equals(entity.getCvType())) roleId=ConstantDic.CUSTOMER_CONTACTER_TYPE_COOP;
//    	if ("2022".equals(entity.getCvType())) roleId=ConstantDic.CUSTOMER_CONTACTER_TYPE_JOB_CONTACTER;
//    	if ("2023".equals(entity.getCvType())) roleId=ConstantDic.CUSTOMER_CONTACTER_TYPE_FINANCE_CONTACTER;
    	
    	if ("2020".equals(ConstantDic.CUSTOMER_CONTACTER_TYPE_CONTACTER)) cvType="2020";
    	if ("2021".equals(ConstantDic.CUSTOMER_CONTACTER_TYPE_COOP)) cvType="2021";
    	if ("2022".equals(ConstantDic.CUSTOMER_CONTACTER_TYPE_JOB_CONTACTER)) cvType="2022";
    	if ("2023".equals(ConstantDic.CUSTOMER_CONTACTER_TYPE_FINANCE_CONTACTER)) cvType="2023";    	
		Cv cv=cvService.getById(customerContacter.getCvId());
		Company company=companyService.getByCustId(customerContacter.getCustId());
		if (cv!=null&&company!=null){
			customerContacter.getCustId();
			cv.setCompanyId(company.getId());
			cv.setCvType(cvType);
			//cv.setCreator(customerContacter.getCreator());
			//cv.setCreateDt(new Date());
			cvService.doCompanyCv(cv);	
		}
		
		
		
	}
	
	@Transactional(readOnly=true)
	public Page findPage(CustomerContacterQuery query) {
		return customerContacterDao.findPage(query);
	}
	public List findCustomContacter(CustomerContacterQuery query) {
		// TODO Auto-generated method stub
		
		
		return customerContacterDao.findCustomContacter(query);
		
		
		//return l;
		
	}
	public Integer getCustIdByCompanyId(Integer companyId) {
		// TODO Auto-generated method stub
		return customerContacterDao.getCustIdByCompanyId(companyId);
	}
	
}
